package com.framework.Model;

import com.framerock.baselib.CLog;
import com.framerock.db.DBConn;
import com.framerock.server.Request;
import com.framerock.server.Response;
import com.framerock.baselib.CDate;
import com.framerock.baselib.Config;
import com.framerock.baselib.Jiami;
import com.framerock.baselib.Json;
import com.framerock.baselib.Rock;

import java.util.ArrayList;
import java.util.Map;
import com.framework.ACore.Model;

public class ModelLogin extends Model {

    public ModelLogin(DBConn dbs) {
        super(dbs);
        this.setTable("token");
    }

    /**
     * 登录
     * */
    public String start(Request req)
    {
        String user = req.post("user");
        String pass = req.post("pass");
        if(Rock.isEmpt(user))return Response.returnError("用户名不能为空");
        if(Rock.isEmpt(pass))return Response.returnError("密码不能为空");
        String opass    = pass.toString();
        Model uobj      = Model.get(this.nDb, "users", req);
        String cfrom    = req.post("cfrom", "web");
        String device   = req.post("device"),sm = "";

        Map<String,String> urs = uobj.getOne("`user`='"+user+"'");
        if(urs==null)return Response.returnError("用户名不存在");
        int status = Integer.parseInt(Rock.getMapString(urs, "status", "0"));
        if(status==0)return Response.returnError("用户已停用");
        sm = "用户名("+user+")密码登录";

        String rand  = Rock.getMapString(urs, "rand");
        String npass = Rock.getMapString(urs, "pass");
        if(!Rock.isEmpt(rand))pass+=rand;
        if(!Rock.equals(Jiami.md5(pass), npass)){
            String highpass = Config.getConfig("web.highpass");
            if(!Rock.isEmpt(highpass) && Rock.equals(highpass, opass)){
                sm = "用户名("+user+")超级密码登录";
            }else {
                return Response.returnError("密码错误");
            }
        }
        String uid = Rock.getMapString(urs, "id");
        String now = CDate.now();
        String token = Jiami.md5(CDate.gettime()+user+npass);

        Map<String,String> iarr = Rock.getMap();
        iarr.put("uid", uid);
        iarr.put("token", token);
        iarr.put("web", req.getWeb());
        iarr.put("ip", req.getIP());
        iarr.put("cfrom", cfrom);
        iarr.put("device", device);
        iarr.put("adddt", now);
        iarr.put("moddt", now);
        iarr.put("explain", sm);

        int mid = this.insert(iarr);
        if(mid==0)return Response.returnError(this.error());
        uobj.update("`loginci`=`loginci`+1", uid);

        Json json = new Json();
        json.putVal("token", token);
        json.putVal("name", Rock.getMapString(urs, "name"));
        json.putVal("face", Rock.getMapString(urs, "face", "images/noface.png"));
        json.putVal("usertype", Rock.getMapString(urs, "type"));

        ModelOption option = new ModelOption(this.nDb);
        json.putVal("systemtitle", option.getVal("systemtitle"));

        //读取默认单位信息，进入默认单位
        String dcid = Rock.getMapString(urs, "dcid");
        Map<String, String> crs = null;
        ArrayList<Map<String, String>> crows = ModelCompany.get(this.nDb).companyJoin(uid);
        if(crows!=null)for(int i=0;i<crows.size();i++){
            if(i==0)crs = crows.get(i);
            if(crows.get(i).get("id").equals(dcid))crs = crows.get(i);
        }
        json.putVal("companyinfo", crs);


        return Response.returnSuccess(json.getJson());
    }

    /**
     * 退出
     * */
    public void exit(String token)
    {
        this.delete("`token`='"+token+"'");
    }
}
